package cc.rengu.igas.channel.nucc.core.service.trans;

import cc.rengu.igas.channel.nucc.common.constant.NuccParamConstant;
import cc.rengu.igas.channel.nucc.common.constant.NuccTreeNodeConstant;
import cc.rengu.igas.channel.nucc.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.nucc.facade.bean.*;
import cc.rengu.igas.channel.nucc.facade.request.TransDetailQueryRequest;
import cc.rengu.igas.channel.nucc.facade.request.TransDetailQueryAcqRequest;
import cc.rengu.igas.channel.nucc.facade.response.TransDetailQueryResponse;
import cc.rengu.igas.channel.nucc.facade.response.TransDetailQueryAcqResponse;
import cc.rengu.igas.channel.nucc.core.realize.NuccService;
import cc.rengu.igas.channel.nucc.core.realize.impl.NuccServiceImpl;
import cc.rengu.oltp.service.base.BizBaseService;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.dao.FinanceTxnMapper;
import cc.rengu.oltp.service.common.dao.impl.FinanceTxnMapperImpl;
import cc.rengu.oltp.service.common.entity.FinanceTxn;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.model.RiskMonitorInfo;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import com.alibaba.fastjson.JSON;

/**
 * 交易详情查询
 */
public class EpccTransDetailQueryService extends BizBaseService {
    @Override
    protected Object beforeBizProcess() throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        TransDetailQueryRequest request = new TransDetailQueryRequest();
        ConvertUtil.convertOutput(request);

        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.FRONT_SEQ_NUM, request.getHeader().getTraceNo());
        return request;
    }

    @Override
    protected BizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        TransDetailQueryRequest innerRequest = (TransDetailQueryRequest) request;
        xmlTreeUtil.setXmlTreeStringValue(NuccTreeNodeConstant.EPCC_ISSR_ID, innerRequest.getInstgId());

        TransDetailQueryAcqRequest acqRequest = initTransDetailQueryAcqRequest(xmlTreeUtil, innerRequest);

        NuccService epccService = new NuccServiceImpl();
        TransDetailQueryAcqResponse acqResponse = epccService.transDetailQuery(acqRequest);
        BizResponse bizResponse = new BizResponse();
        bizResponse.setRspSysId(NuccParamConstant.IGAS_SYS_ID);
        if (!NuccParamConstant.EPCC_SUCCESS_CODE.equals(acqResponse.getSysRtnCd())) {
            rglog.error("获取订单详情失败，respCode=[{}],respDesc=[{}]", acqResponse.getSysRtnCd(), acqResponse.getSysRtnDesc());
            bizResponse.setRespCode(acqResponse.getSysRtnCd());
            bizResponse.setRespDesc(acqResponse.getSysRtnDesc());
        } else {
            BizInfBean bizInf = new BizInfBean();
            bizInf.setOriSysRtnCd(acqResponse.getOriSysRtnCd());
            bizInf.setOriSysRtnDesc(acqResponse.getOriSysRtnDesc());
            bizInf.setOriBizStsCd(acqResponse.getOriBizStsCd());
            bizInf.setOriBizStsDesc(acqResponse.getOriBizStsDesc());
            bizInf.setOriTrxCtgy(acqResponse.getOriTrxCtgy());
            //返回原渠道流水
            FinanceTxn orgFinanceTxn = (FinanceTxn) xmlTreeUtil.getXmlTreeObjectValue(NuccTreeNodeConstant.ORG_TXN_INFO, FinanceTxn.class);
            bizInf.setOriTrxId(orgFinanceTxn.getFrontSeqNum());
            bizInf.setOriTrxAmt(acqResponse.getOriTrxAmt());
            bizInf.setOriTrxStatus(acqResponse.getOriTrxStatus());
            bizInf.setOriTrxBankId(acqResponse.getOriBatchId());

            OriTrxInfoBean oriTrxInf = new OriTrxInfoBean();
            oriTrxInf.setDbtrBankId(acqResponse.getOriTrxBkInf().getDbtrBankId());
            oriTrxInf.setDbtrBankAmt(acqResponse.getOriTrxBkInf().getDbtrBankAmt());
            oriTrxInf.setDbtrBankTrxSts(acqResponse.getOriTrxBkInf().getDbtrBankTrxSts());
            oriTrxInf.setDbtrBankTrxDtTm(acqResponse.getOriTrxBkInf().getDbtrBankTrxDtTm());
            oriTrxInf.setCdtrBankId(acqResponse.getOriTrxBkInf().getCdtrBankId());
            oriTrxInf.setCdtrBankAmt(acqResponse.getOriTrxBkInf().getCbtrBankAmt());
            oriTrxInf.setCdtrBankTrxSts(acqResponse.getOriTrxBkInf().getCbtrBankTrxSts());
            oriTrxInf.setCdtrBankTrxDtTm(acqResponse.getOriTrxBkInf().getCbtrBankTrxDtTm());

            PyerInfBean pyerInf = new PyerInfBean();
            pyerInf.setPyerAcctIssrId(acqResponse.getPyerInf().getPyerAcctIssrId());
            pyerInf.setPyerAcctTp(acqResponse.getPyerInf().getPyerAcctTp());
            pyerInf.setSgnNo(acqResponse.getPyerInf().getSgnNo());
            pyerInf.setPyerAcctId(acqResponse.getPyerInf().getPyerAcctId());
            pyerInf.setPyerAcctNm(acqResponse.getPyerInf().getPyerAcctNm());
            pyerInf.setMchntNo(acqResponse.getPyerInf().getMchntNo());
            pyerInf.setMchntNm(acqResponse.getPyerInf().getMchntNm());

            PyeeInfBean pyeeInf = new PyeeInfBean();
            pyeeInf.setPyeeAcctIssrId(acqResponse.getPyeeInf().getPyeeAcctIssrId());
            pyeeInf.setPyeeAcctTp(acqResponse.getPyeeInf().getPyeeAcctTp());
            pyeeInf.setSgnNo(acqResponse.getPyeeInf().getSgnNo());
            pyeeInf.setPyeeAcctId(acqResponse.getPyeeInf().getPyeeAcctId());
            pyeeInf.setPyeeAcctNm(acqResponse.getPyeeInf().getPyeeAcctNm());

            PayInfBean payInf = new PayInfBean();
            payInf.setOrdrId(acqResponse.getPayInf().getOrdrId());
            payInf.setOrderDetails(acqResponse.getPayInf().getOrderDetails());
            payInf.setPyeeAddr(acqResponse.getPayInf().getPyeeAddr());

            TransDetailQueryResponse response = new TransDetailQueryResponse();
            response.setBizInf(bizInf);
            response.setOriTrxInf(oriTrxInf);
            response.setPyerInf(pyerInf);
            response.setPyeeInf(pyeeInf);
            response.setPayInf(payInf);
            String JSONString = JSON.toJSONString(acqResponse.getPayInf().getRfndInfList());
            response.setRfndInfList(JSONString);
            response.setResfdAcctIssrId(innerRequest.getInstgId());
            response.setInstgAcctId(acqResponse.getInstgAcctId());
            response.setInstgAcctNm(acqResponse.getInstgAcctNm());
            response.setClrSts(acqResponse.getClrSts());
            response.setClrDtTm(acqResponse.getClrDtTm());
            response.setTrxSmmry(acqResponse.getTrxSmmry());
            response.setTrxUstrd(acqResponse.getTrxUstrd());

            bizResponse.setRespCode(acqResponse.getBizStsCd());
            bizResponse.setRespDesc(acqResponse.getBizStsDesc());
            bizResponse.setResult(response);
        }

        return bizResponse;
    }

    @Override
    protected void afterBizProcess(BizResponse response) throws Exception {
        TransDetailQueryResponse transDetailQueryResponse = (TransDetailQueryResponse) response.getResult();
        ConvertUtil.convertInput(transDetailQueryResponse);
    }

    @Override
    protected RiskMonitorInfo getRiskMonitorInfo() {
        return null;
    }

    private TransDetailQueryAcqRequest initTransDetailQueryAcqRequest(XmlTreeUtil xmlTreeUtil, TransDetailQueryRequest innerRequest) throws Exception {
        TransDetailQueryAcqRequest request = new TransDetailQueryAcqRequest();
        request.setSinkInstgId(innerRequest.getSinkInstgId());
        request.setOriInstgId(innerRequest.getInstgId());
        request.setOriTrxCtgy(innerRequest.getOriTrxCtgy());

        /*从原流水表获取网联流水号*/
        String oriTrxId = innerRequest.getOriTrxId();
        String oriTrxDtTm = innerRequest.getOriTrxDtTm();
        String instId = innerRequest.getHeader().getInstId();
        FinanceTxnMapper txnMapper = new FinanceTxnMapperImpl();
        FinanceTxn orgFinanceTxn = txnMapper.selectFinanceTxnInfoByFrontSeqNum(instId, oriTrxDtTm, oriTrxId, NuccParamConstant.TUNL_ID_NUCC, innerRequest.getHeader().getChanlId());
        if (null == orgFinanceTxn) {
            rglog.error("根据前端流水号[{}]查询原交易失败!", oriTrxId);
            throw new BizException(RespCodeEnum.ORIG_TXN_NOT_FOUND.getRespCode(), RespCodeEnum.ORIG_TXN_NOT_FOUND.getRespDesc());
        }
        xmlTreeUtil.setXmlTreeObjectValue(NuccTreeNodeConstant.ORG_TXN_INFO, orgFinanceTxn);

        request.setOriTrxId(orgFinanceTxn.getRemark1());

        /*原交易日期时间从原交易系统流水号获取*/
        String srcDateTime = request.getOriTrxId().substring(0, 8) + request.getOriTrxId().substring(12, 18);
        String trxDateTime = DateUtil.dateStringFormatConvert(srcDateTime, "yyyyMMddHHmmss", "yyyy-MM-dd'T'HH:mm:ss");
        request.setOriTrxDtTm(trxDateTime);
        request.setOriAcctTp(innerRequest.getOriAcctTp());
        request.setOriSgnNo(innerRequest.getOriSgnNo());

        return request;
    }
}
